#ifndef APPLICATIONSTATEMANAGER_H
#define APPLICATIONSTATEMANAGER_H

#include <QObject>
#include <memory>
#include "statusbarinfo.h"
#include "statusbarstate.h"
#include "processingsettings.h"
#include "exportprogress.h"
#include "istatusupdatestrategy.h"

class StatusBarManager;
class CustomFileBrowser;

/**
 * ApplicationStateManager - 应用状态管理器
 *
 * Martin Fowler重构：Replace Data Clumps with Value Object
 * 使用统一的StatusBarState替代分散的状态参数，建立单一数据源原则
 * 集中管理应用程序的状态更新，避免状态更新逻辑分散在多个类中
 */
class ApplicationStateManager : public QObject
{
    Q_OBJECT

public:
    explicit ApplicationStateManager(QObject* parent = nullptr);

    // 注册组件
    void setStatusBarManager(StatusBarManager* statusBarManager);
    void setFileBrowser(CustomFileBrowser* fileBrowser);

    // Martin Fowler重构：统一状态更新方法 - Replace Parameter Object
    void updateCurrentFileStatus(int currentIndex, int totalFiles, const QString& fileName);
    void updatePageStatus(int currentPage, int totalPages);
    void updateProcessingSettings(const ProcessingSettings& settings);
    void updateCacheStatus(qint64 cacheSize, int cacheCount);
    void updateSystemStatus(const QString& status);
    void updateExportProgress(const ExportProgress& exportProgress);

    // 完整状态更新 - 新增：单一更新点
    void updateCompleteState(const StatusBarState& newState);

    // Martin Fowler重构：Strategy Pattern - 状态更新策略管理
    void setUpdateStrategy(std::unique_ptr<IStatusUpdateStrategy> strategy);
    IStatusUpdateStrategy* currentUpdateStrategy() const { return m_updateStrategy.get(); }

    // 状态查询 - Martin Fowler重构：使用统一状态对象
    const StatusBarState& currentState() const { return m_currentState; }
    StatusBarInfo getCurrentFileStatus() const { return m_currentState.fileInfo(); }
    bool hasFiles() const { return m_currentState.hasFiles(); }
    bool canExport() const { return m_currentState.canExport(); }

signals:
    // Martin Fowler重构：统一状态变化信号
    void stateChanged(const StatusBarState& newState, const StatusBarState& oldState);

    // 向后兼容的具体状态信号
    void fileStatusChanged(const StatusBarInfo& statusInfo);
    void pageStatusChanged(int currentPage, int totalPages);
    void cacheStatusChanged(qint64 cacheSize, int cacheCount);
    void systemStatusChanged(const QString& status);
    void exportProgressChanged(const ExportProgress& exportProgress);

private:
    // Martin Fowler重构：Extract Method - 统一状态更新逻辑
    void applyStateChange(const StatusBarState& newState);
    void notifySpecificChanges(const StatusBarState& newState, const StatusBarState& oldState);

    // 组件引用
    StatusBarManager* m_statusBarManager;
    CustomFileBrowser* m_fileBrowser;

    // Martin Fowler重构：Replace Data Clumps - 统一状态对象
    StatusBarState m_currentState;

    // Martin Fowler重构：Strategy Pattern - 状态更新策略
    std::unique_ptr<IStatusUpdateStrategy> m_updateStrategy;
};

#endif // APPLICATIONSTATEMANAGER_H